<?php

namespace plugins\fund\controller;

use app\common\lib\model_operate\Transit; //引入此类

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;

/**
 * 需求配置首页
 */
class AdminIndexController extends PluginAdminBaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {

    }

    /**
     * 流水首页
     */
    public function index()
    {

        $data = $this->request->param();

        $pagesize = !empty($data['pagesize']) ? $data['pagesize'] : 10;

        $search = Transit::createTags('search');

        $search->setParam($data);

        $table = Transit::createTags('table');

        $table->setParam($search);

        //店铺id过滤 商家只看到所属的流水

        // $is_admin = Common::getAllowAccess();

        $where = [];

        // if (true !== $is_admin) {

        //     $where['store_id'] = ['=', $is_admin];

        // }

        $list = $table->query()->where($where)->paginates($pagesize);

        $this->assign('table', $table);

        $list->appends($data);

        $this->assign('searchStr', $search->createHtml());

        $this->assign('list', $list);

        $this->assign('page', $list->render());

        return $this->fetch();

    }

    /**
     * 用户账户列表
     */
    public function accountList()
    {
        $data = $this->request->param();
        $list = Db::name("fund_account")->paginates(20);
        $list->appends($data);
        
        $this->assign('page', $list->render());
        $this->assign('list', $list);
        $this->assign('search', $data);

        return $this->fetch('admin_index/accountList');
    }

    /**
     * 银行信息
     */
    public function banklist()
    {

        $data = $this->request->param();

        $where = 1;

        if (!empty($data['types']) && !empty($data['category'])) {

            $types = $data['types'];

            $category = "'%" . $data['category'] . "%'";

            if ($types == 1) {

                $where .= " and `bank` like" . $category;

            }

            if ($types == 2) {

                $where .= " and `card_bin` like" . $category;

            }

            if ($types == 3) {

                $where .= " and `desc` like" . $category;

            }

        }

        $da = Db::name("fund_bank")->where($where)->paginates(20);

        $da->appends($data);

        $this->assign('page', $da->render());

        $this->assign('data', $da);

        $this->assign('search', $data);

        return $this->fetch();

    }

    /**

     * 删除账户

     */

    public function delAccount()
    {

        $data = $this->request->param();

        if (empty($data['id'])) {

            $this->error('请选择需要删除的数据！');

        }

        if (is_array($data['id'])) {

            $da = Db::name('fund_bankcard')->where('id', 'in', $data['id'])->delete();

        } else {

            $da = Db::name("fund_bankcard")->where("id", $data['id'])->delete();

        }

        if ($da) {

            return $this->success("删除成功");

        } else {

            return $this->error('删除失败');

        }

    }

    /**

     * 添加图片

     */

    public function addphoto()
    {

        $file = $_FILES;

        $path = '/plugins/fund/view/public/upload/banklogo';

        $res = uploadImg($file['photo'], $path);

        if ($res['status'] == false) {

            $this->error('上传失败');

        }

        $this->success('上传成功', '', $res['data']);

    }

    /**

     * 获取银行数据

     */

    public function fund_bank()
    {

        $data = $this->request->param();

        $da = Db::name("fund_bank")->where('id', $data['id'])->find();

        return $da;

    }

    /**

     * 更该银行数据

     */

    public function upbank()
    {

        $data = $this->request->param();

        $da = Db::name("fund_bank")->where('id', $data['id'])->update(['desc' => $data['desc'], 'thumb' => $data['thumb'], 'bank' => $data['bank'], 'status' => $data['powerExp']]);

        if ($da) {

            $this->success('修改成功');

        } else {

            $this->error('修改失败');

        }

    }

    /*

     * 添加银行

     */

    public function addbank()
    {

        $data = $this->request->param();

        $da['desc'] = $data['desc'];

        $da['thumb'] = $data['thumb'];

        $da['bank'] = $data['bank'];

        $da['status'] = $data['powerExp'];

        $data = Db::name("fund_bank")->insert($da);

        if ($data) {

            $this->success('添加成功');

        } else {

            $this->error('添加失败');

        }

    }

    /**



     * 删除银行



     */

    public function delbank()
    {

        $data = $this->request->param();

        if (empty($data['id'])) {

            $this->error('请选择需要删除的数据！');

        }

        if (is_array($data['id'])) {

            $da = Db::name('fund_bank')->where('id', 'in', $data['id'])->delete();

        } else {

            $da = Db::name("fund_bank")->where("id", $data['id'])->delete();

        }

        if ($da) {

            return $this->success("删除成功");

        } else {

            return $this->error('删除失败');

        }

    }

    /**

     * 提现审核

     */

    public function presentation()
    {

        $data = $this->request->param();

        $where = 1;

        if (!empty($data['keywords'])) {

            $where .= " and accountname like '%" . $data['keywords'] . "%' or trade_sn like '%" . $data['keywords'] . "%'";

        }

        if (!empty($data['start_time'])) {

            $where .= " and uptime >= " . strtotime($data['start_time']);

        }

        if (!empty($data['end_time'])) {

            $where .= " and uptime <= " . strtotime($data['end_time']);

        }

        $da = Db::name("fund_tx_record")->where($where)->order('status', 'asc')->order('addtime', 'desc')->paginate(20);

        foreach ($da as $key => $value) {

            //     // $type  = Db::name("fund_type")->where('type',1)->where('zid',$value['type'])->select();

            $count = 0;

            //     foreach( $type as $t){

            //          $count+=$t['number'];

            //     }

            $value['number'] = $count;

            $da[$key] = $value;

        }

        $contype = ['1' => '支付宝', '2' => '微信', '3' => '银行卡'];

        $this->assign('list', $da);

        // 在 render 前，使用appends方法保持分页条件

        $da->appends($data);

        $this->assign('page', $da->render()); //单独提取分页出来

        $this->assign('data', $data);

        $this->assign('contype', $contype);

        return $this->fetch();

    }

}
